export default {
    state: {
        is_set_user_login: 0,
        is_realname: 0,
        is_seller_firm: 0,
        is_annual_fee_end_date_seller_firm: 0,
        is_pay_open_express_seller_firm: 0,
        seller_firm_checkbox_values: {},
        auth_token: null,
        identity_info: {},
        user_asset_info: {},

        user_asset_log_types: null,
        user_asset_log_date_start: '2020-09-25',

        dict_to_shop_same_city_express: {}
    },
    mutations: {
        // 用户登录成功后保存用户信息
        userLogin(state, data) {
            let is_set_user_login = state.is_set_user_login;
            state.is_set_user_login = 0;

            state.auth_token = data.auth_token;
            uni.setStorageSync('user_auth_token', data.auth_token);
            this.commit('setIdentityInfo', data.login_info.identity_info);
            this.commit('setUserAssetInfo', data.login_info.user_asset_info);

            if (is_set_user_login == 0) {
                if (data.is_change_login_seller != true) {
                    uni.removeStorageSync('user_login_info');
                }
                this.commit('userIsLoginToMyPage', {
                    message: data.message,
                    // #ifdef APP-PLUS
                    is_login: '1',
                    // #endif
                })
                return;
            }
        },
        // 设置用户登录状态
        setUserLogin(state, data) {
            state.is_set_user_login = 1;
            this.commit('userLogin', data);
        },
        // 用户退出登录 清除用户登录信息 转换到登录页面
        userLogout(state) {
            uni.removeStorageSync('user_login_info');
            this.commit('userClearLoginInfo');
            this.commit('userNotLoginToLoginPage');
        },
        // 清除用户登录信息
        userClearLoginInfo(state) {
            state.is_set_user_login = 0;
            state.is_realname = 0;
            state.is_seller_firm = 0;
            state.is_annual_fee_end_date_seller_firm = 0;
            state.is_pay_open_express_seller_firm = 0;
            state.seller_firm_checkbox_values = {};
            state.auth_token = null;
            state.identity_info = {};
            state.user_asset_info = {};

            state.user_asset_log_types = null;
            state.dict_to_shop_same_city_express = {};

            uni.removeStorageSync('user_auth_token')
            uni.removeStorageSync('user_identity_info')
            uni.removeStorageSync('user_asset_info')
        },
        // 改变登录成商家
        userChangeLoginSeller(state, data) {
            uni.setStorageSync('user_login_info', {
                auth_token: uni.getStorageSync('user_auth_token'),
                login_info: {
                    identity_info: uni.getStorageSync('user_identity_info'),
                    user_asset_info: uni.getStorageSync('user_asset_info')
                }
            })
            this.commit('userClearLoginInfo');
            this.commit('userLogin', Object.assign(data, {
                is_change_login_seller: true,
                message: '账号切换成功'
            }));
        },
        // 改变登录成用户
        userChangeLoginUser() {
            let user_login_info = uni.getStorageSync('user_login_info');
            this.commit('userClearLoginInfo');
            this.commit('userLogin', Object.assign(user_login_info, {
                is_change_login_user: true,
                message: '账号切换成功'
            }));
        },
        // 用户重新登录
        userRelogin(state) {
            this.commit('userLogout');
        },
        // 保存用户信息
        setIdentityInfo(state, _identity_info) {
            let identity_info = Object.assign({}, _identity_info);
            // 记录用户有没有实名
            if (identity_info.idcard &&
                identity_info.truename &&
                identity_info.area_code &&
                identity_info.alipay_account) {
                state.is_realname = 1;
            }
            if (identity_info.is_seller == 1 || identity_info.is_firm == 1) {
                state.is_seller_firm = 1;
                if (identity_info.is_seller == 1) {
                    state.seller_firm_checkbox_values.is_seller = '商家';
                }
                if (identity_info.is_firm == 1) {
                    state.seller_firm_checkbox_values.is_firm = '企业';
                }
            }
            if (identity_info.seller_info) {
                if (identity_info.seller_info.annual_fee_end_date > 0 || identity_info.seller_info.annual_fee_end_date_firm >
                    0) {
                    state.is_annual_fee_end_date_seller_firm = 1;
                }
                if (identity_info.seller_info.is_pay_open_express > 0 || identity_info.seller_info.is_pay_open_express_firm >
                    0) {
                    state.is_pay_open_express_seller_firm = 1;
                }
                state.dict_to_shop_same_city_express = {};
                if (identity_info.seller_info.is_express > 0) {
                    state.dict_to_shop_same_city_express['is_express'] = '全国邮寄';
                }
                if (identity_info.seller_info.is_same_city > 0) {
                    state.dict_to_shop_same_city_express['is_same_city'] = '同城配送';
                }
                if (identity_info.seller_info.is_to_shop > 0) {
                    state.dict_to_shop_same_city_express['is_to_shop'] = '到店自取';
                }
            }
            state.identity_info = identity_info;
            uni.setStorageSync('user_identity_info', identity_info)
        },
        // 保存用户资产信息
        setUserAssetInfo(state, _user_asset_info) {
            let user_asset_info = Object.assign({}, _user_asset_info);
            state.user_asset_info = user_asset_info;
            uni.setStorageSync('user_asset_info', user_asset_info)
        },
        // 用户已登录，跳转到我的页面
        userIsLoginToMyPage(state, data) {
            if (!state.auth_token) return;
            let message = null;
            let is_login = null;
            if (data) {
                if (data.message) {
                    message = data.message;
                }
                if (data.is_login == 1) {
                    is_login = data.is_login;
                }
            }
            this.commit('userLaunchTo', '/page/index?nav=my' +
                (message ? '&message=' + message : '') +
                (is_login ? '&is_login=' + is_login : '')
            );
        },
        // 用户未登录，跳转到登录页面
        userNotLoginToLoginPage(state) {
            if (state.auth_token) return;
            this.commit('userLaunchTo', '/account/login');
        },
        // 用户实名页面
        userNavToTruename() {
            this.commit('userNavTo', '/user/info/info');
        },
        userWindowOpen(state, url) {
            if (!url) return;
            // #ifdef H5
            return window.open(url);
            // #endif
        },
        userNavTo(state, uri) {
            if (!uri) return;
            uni.navigateTo({
                url: '/pages' + uri
            })
        },
        userReTo(state, uri) {
            if (!uri) return;
            uni.redirectTo({
                url: '/pages' + uri
            })
        },
        userLaunchTo(state, uri) {
            if (!uri) return;
            uni.reLaunch({
                url: '/pages' + uri
            });
        },
        userToastNone(state, message) {
            uni.showToast({
                title: message,
                duration: 3000,
                icon: 'none'
            });
        },
        userToastSuccess(state, data) {
            let message, reToUri, commit, callback, mask = false;
            if (typeof(data) == 'object') {
                message = data.message;
                reToUri = data.reToUri;
                commit = data.commit;
                callback = data.callback;
            } else {
                message = data;
            }
            if (callback) {
                mask = true;
                setTimeout(() => {
                    callback();
                }, 1000);
            }
            if (commit) {
                mask = true;
                setTimeout(() => {
                    this.commit(commit);
                }, 1000);
            }
            if (reToUri) {
                mask = true;
                setTimeout(() => {
                    this.commit('userReTo', reToUri);
                }, 1000);
            }
            uni.showToast({
                title: message,
                duration: 2000,
                icon: 'success',
                mask: mask
            });
        },
    },
    actions: {},
    getters: {}
}
